<?php
/**[文档属性表模型]
 * @Author: 976123967@qq.com
 * @Date:   2014-11-10 13:10:39
 * @Last Modified by:   chenryoku
 * @Last Modified time: 2014-11-16 10:30:42
 */
class ArticleAttrModel extends Model{

	protected $tableName='article_attr';


	/**
	 * [alert_article_attr 修改文档属性]
	 * @param [type] $aid [description]
	 */
	public function alert_article_attr($aid)
	{
		$cid = I('post.category_cid');
		$articleAttr = $_POST['article_attr'];

		// 删除原先的数据
		$this->where(array('article_aid'=>$aid))->delete();
		//p($articleAttr);

		$attrValueModel = D('AttrValue');
		foreach($articleAttr as $k=>$v)
		{
			$temp = explode('-', $k);

			foreach ($v as  $value) 
			{
				
				$attrValueId = $attrValueModel->get_field_by_attr_value($value,'attr_value_id');
				//p($temp);
				
				$data = array(

					'article_aid'=>$aid,
					'category_cid'=>$cid,
					'attr_attr_id'=>$temp[0],
					'type'=>$temp[1],
					'type_typeid'=>$temp[2],
					'is_pic'=>$temp[3],
					'attr_value_attr_value_id'=>$attrValueId,
					'attr_value'=>$value,
				);
				// 验证是否有主键，目的要保留原来的值文档属性主键值article_attr_id
				if($temp[4])
						$data['article_attr_id'] = $temp[4];
				
				$this->add($data);
			}	
		}
	
	}


	/**
	 * [find_article_attr 获取属性值]
	 * @return [type] [description]
	 */
	public function  find_article_attr($aid)
	{

		return $this->where(array('article_aid'=>$aid))->getField('attr_value_attr_value_id',true);
	}


	/**
	 * [delete_article_attr 删除文档属性值]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function delete_article_attr($aid)
	{
		$this->where(array('article_aid'=>$aid))->delete();
	}

	/**
	 * [get_field_by_attr_value_id 获得指定的字段数据通过属性值id和文档aid]
	 * @param  [type] $attrValueId [description]
	 * @param  [type] $aid         [description]
	 * @param  string $field       [description]
	 * @return [type]              [description]
	 */
	public function get_field_by_attr_value_id($attrValueId,$aid,$field='article_attr_id')
	{
		return $this->where(array('article_aid'=>$aid,'attr_value_attr_value_id'=>$attrValueId))->getField($field);
	}



}